package com.booktrading.service.impl;

import com.booktrading.mapper.BookMapper;
import com.booktrading.mapper.OrderMapper;
import com.booktrading.model.Book;
import com.booktrading.model.Order;
import com.booktrading.service.DashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, Integer> getUserDashboardStats(Long userId) {
        Map<String, Integer> stats = new HashMap<>();
        
        // 1. 获取在售书籍数量
        Book condition = new Book();
        condition.setSellerId(userId);
        condition.setStatus(1); // 1-在售
        List<Book> onSaleBooks = bookMapper.selectByCondition(condition);
        stats.put("onSaleBooks", onSaleBooks.size());
        
        // 2. 获取已售出书籍数量
        condition.setStatus(2); // 2-已售
        List<Book> soldBooks = bookMapper.selectByCondition(condition);
        stats.put("soldBooks", soldBooks.size());
        
        // 3. 获取待处理订单数量（待发货订单）
        List<Order> sellerOrders = orderMapper.selectBySellerId(userId);
        int pendingOrders = (int) sellerOrders.stream()
                .filter(order -> order.getStatus() == 1) // 1-待发货
                .count();
        stats.put("pendingOrders", pendingOrders);
        
        // 4. 获取已完成订单数量
        int completedOrders = (int) sellerOrders.stream()
                .filter(order -> order.getStatus() == 3) // 3-已完成
                .count();
        stats.put("completedOrders", completedOrders);
        
        return stats;
    }
} 